﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public sealed class Physical2DEventLinstener : MonoBehaviour {

        public delegate void VoidDelegate(GameObject otherGo);

        public VoidDelegate onTriggerEnter2D;
        public VoidDelegate onTriggerStay2D;
        public VoidDelegate onTriggerExit2D;
        public VoidDelegate onCollisionEnter2D;
        public VoidDelegate onCollisionStay2D;
        public VoidDelegate onCollisionExit2D;

        void OnTriggerEnter2D(Collider2D collision) {
            onTriggerEnter2D?.Invoke(collision.gameObject);
        }

        void OnTriggerStay2D(Collider2D collision) {
            onTriggerStay2D?.Invoke(collision.gameObject);
        }

        void OnTriggerExit2D(Collider2D collision) {
            onTriggerExit2D?.Invoke(collision.gameObject);
        }

        void OnCollisionEnter2D(Collision2D collision) {
            onCollisionEnter2D?.Invoke(collision.gameObject);
        }

        void OnCollisionStay2D(Collision2D collision) {
            onCollisionStay2D?.Invoke(collision.gameObject);
        }

        void OnCollisionExit2D(Collision2D collision) {
            onCollisionExit2D?.Invoke(collision.gameObject);
        }

        public static Physical2DEventLinstener Get(GameObject thisGo) {
            Physical2DEventLinstener listener = thisGo.GetComponent<Physical2DEventLinstener>();
            if (listener == null) {
                listener = thisGo.AddComponent<Physical2DEventLinstener>();
            }
            return listener;
        }

        void OnDestroy() {
            onTriggerEnter2D = null;
            onTriggerStay2D = null;
            onTriggerExit2D = null;
            onCollisionEnter2D = null;
            onCollisionStay2D = null;
            onCollisionExit2D = null;
        }

    }

}